package panda.li.navigation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.google.gson.internal.LinkedTreeMap;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import panda.li.navigation.R;
import panda.li.navigation.adapter.BusLineAdapter;
import panda.li.navigation.adapter.NewsAdapter;
import panda.li.navigation.base.ActivityCollector;
import panda.li.navigation.base.BaseActivity;
import panda.li.navigation.base.MyApplication;
import panda.li.navigation.callback.BaseCallBack;
import panda.li.navigation.callback.BusLineCallBack;
import panda.li.navigation.constant.Constant;
import panda.li.navigation.entity.BusLine;
import panda.li.navigation.entity.NewsInfo;
import panda.li.navigation.utils.AMapUtil;
import panda.li.navigation.utils.Result;
import panda.li.navigation.utils.ToastUtil;

/**
 */
public class NewsActivity extends BaseActivity {
    @Bind(R.id.lv_data)
    ListView lv_data;

    @Bind(R.id.btn_road)
    Button btn_road;

    @Bind(R.id.btn_traffic)
    Button btn_traffic;

    @Bind(R.id.btn_policy)
    Button btn_policy;

    @Bind(R.id.btn_way)
    Button btn_way;

    @Bind(R.id.btn_alarm)
    Button btn_alarm;

    private int upDown;
    private NewsAdapter adapter;
    private List<NewsInfo> newsList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        ButterKnife.bind(this);

        loadData(0);
    }


    @OnClick({R.id.btn_alarm, R.id.btn_road, R.id.btn_traffic, R.id.btn_policy, R.id.btn_way})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_road:
                loadData(0);
                break;
            case R.id.btn_traffic:
                loadData(1);
                break;
            case R.id.btn_policy:
                loadData(2);
                break;
            case R.id.btn_way:
                loadData(3);
                break;
            case R.id.btn_alarm:
                loadData(4);
                break;
        }

    }

    private void loadData(int type) {

        OkHttpUtils.post().url(Constant.LOAD_NEWS_LIST)
        .addParams("kind", String.valueOf(type))
        .build()
        .execute(new BaseCallBack<Result>() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show(MyApplication.getInstance(), e.getMessage());
            }

            @Override
            public void onSuccess(Result response, int ids) {
                if (response != null) {
                    List<com.google.gson.internal.LinkedTreeMap> datalist = (ArrayList<LinkedTreeMap>) response.getData();
                    try {
                        newsList = new ArrayList<>();
                        for (com.google.gson.internal.LinkedTreeMap map: datalist) {
                            Double id = (Double) map.get("id");
                            String kind = (String) map.get("kind");
                            String title = (String) map.get("title");
                            String content = (String) map.get("content");
                            Double clicks = (Double) map.get("clicks");
                            Double sortno = (Double) map.get("sortno");
                            String remark = (String) map.get("remark");
                            String createdate = (String) map.get("createdate");
                            NewsInfo newsInfo = new NewsInfo();
                            newsInfo.setId(id.intValue());
                            newsInfo.setKind(kind);
                            newsInfo.setTitle(title);
                            newsInfo.setContent(content);
                            newsInfo.setClicks(clicks.intValue());
                            newsInfo.setSortno(sortno.intValue());
                            newsInfo.setRemark(remark);
                            newsInfo.setCreatedate(AMapUtil.convertToTime(createdate));
                            newsList.add(newsInfo);
                        }

                        if(newsList.size() == 0){
                            ToastUtil.show(getApplicationContext(), "暂无数据！！！");
                        }

                        adapter = new NewsAdapter(getApplicationContext(), newsList);
                        lv_data.setAdapter(adapter);
                        lv_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent(NewsActivity.this, NewsInfoActivity.class);
                                NewsInfo newsInfo = newsList.get(i);
                                intent.putExtra("newsInfo", (Serializable) newsInfo);
                                // beanList = (List<BusLine.BusLinesBean>) getIntent().getSerializableExtra("list");
                                startActivity(intent);
                            }
                        });

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }

        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
